<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { PropType, reactive, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { SysApiDetailVo } from '@/api/system/api/SysApi/types'
import { isBlank } from '@/utils/StringUtils'
import { useBaseWriteView } from '@/base/views/BaseWriteView'
import { sysDictChooseListApi } from '@/api/system/dict/SysDict'
import { KeyLabelValueVo } from '@/base/api/KeyLabelValueVo'
// [[ OnflowGenerator Range (- selectFunctionImport -) Start ]]

// [[ OnflowGenerator Range (- selectFunctionImport -) End ]]

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<SysApiDetailVo>>,
    default: () => null
  },
  editType: {
    type: String,
    default: 'add'
  },
  editId: {
    type: Number,
    default: -1
  }
})
const isAdd = ref(props.editType === 'add')

const { formRegister, submit, doWatch } = useBaseWriteView(props)
doWatch()

const formRef = ref<typeof Form>()
// [[ OnflowGenerator Range (- dictFunctions -) Start ]]
const platformTypeOptions = ref<Array<KeyLabelValueVo>>([])
const getPlatformType = async () => {
  const res = await sysDictChooseListApi('platformType')
  platformTypeOptions.value = res.data
}
getPlatformType()
const controlTypeOptions = ref<Array<KeyLabelValueVo>>([])
const getControlType = async () => {
  const res = await sysDictChooseListApi('sysApiControlType')
  controlTypeOptions.value = res.data
}
getControlType()

// [[ OnflowGenerator Range (- dictFunctions -) End ]]

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]

// [[ OnflowGenerator Range (- selectFunctions -) End ]]

const schema = reactive<FormSchema[]>([
  // [[ OnflowGenerator Range (- inputSchema -) Start ]]
  {
    field: 'platformType',
    label: '平台类型',
    value: 'onflow-backend',
    component: 'Select',
    componentProps: {
      placeholder: '请选择平台类型',
      clearable: true,
      filterable: true,
      options: platformTypeOptions
    }
  },
  {
    field: 'name',
    label: '接口名称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入接口名称',
      clearable: true
    }
  },
  {
    field: 'url',
    label: '接口地址',
    component: 'Input',
    componentProps: {
      placeholder: '请输入接口地址',
      clearable: true
    }
  },
  {
    field: 'controlType',
    label: '接口控制类型',
    value: '0',
    component: 'Select',
    componentProps: {
      placeholder: '请选择接口控制类型',
      clearable: true,
      filterable: true,
      options: controlTypeOptions
    }
  },
  {
    field: 'method',
    label: '请求方式',
    value: 'POST',
    component: 'Input',
    componentProps: {
      placeholder: '请输入请求方式',
      clearable: true
    }
  },
  {
    field: 'moduleName',
    label: '所属模块',
    component: 'Input',
    componentProps: {
      placeholder: '请输入所属模块',
      clearable: true
    }
  },
  {
    field: 'enable',
    label: '是否启用',
    value: true,
    component: 'Switch',
    componentProps: {
      placeholder: '请选择是否启用'
    }
  },
  {
    field: 'remark',
    label: '备注',
    component: 'Input',
    componentProps: {
      placeholder: '请输入备注',
      clearable: true
    }
  },
  // [[ OnflowGenerator Range (- inputSchema -) End ]]
  {
    field: 'createTime',
    label: '创建时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无创建时间',
      disabled: true
    }
  },
  {
    field: 'updateTime',
    label: '更新时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无更新时间',
      disabled: true
    }
  }
])

const rules = reactive({
  // [[ OnflowGenerator Range (- rules -) Start ]]
  platformType: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 64) {
          callback(new Error('平台类型不能超过64个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  name: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 200) {
          callback(new Error('接口名称不能超过200个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  url: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 200) {
          callback(new Error('接口地址不能超过200个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  controlType: [required()],
  method: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 10) {
          callback(new Error('请求方式不能超过10个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  moduleName: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('所属模块不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  enable: [required()],
  remark: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 500) {
          callback(new Error('备注不能超过500个字符'))
        } else {
          callback()
        }
      }
    }
  ]
  // [[ OnflowGenerator Range (- rules -) End ]]
})

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="schema" ref="formRef" />
</template>
